<?xml version="1.0" encoding="UTF-8"?>
<%@page contentType="text/html; charset=UTF-8"
		import="org.apache.commons.lang.StringUtils,
				org.apache.commons.collections.CollectionUtils,
				org.hibernate.Criteria,
				org.hibernate.Hibernate,
				org.hibernate.criterion.Restrictions" %>
<%
	Collection<Book> resultBooks = null;
	Collection<Category> resultCategories = null;

	String q = request.getParameter("q");
	if (q == null) {
		q = StringUtils.EMPTY;
	
	} else {
		Session hSession = HibernateUtil.getSessionFactory().getCurrentSession();
		
		Criteria crit = hSession.createCriteria(Book.class);
		crit.add(Restrictions.ilike("title", "%" + q + "%"));
		resultBooks = crit.list();
		
		crit = hSession.createCriteria(Category.class);
		crit.add(Restrictions.ilike("title", "%" + q + "%"));
		resultCategories = crit.list();
	}
	
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
<head>
	<title>Bookstore :: Recherche de &laquo; <%=q%> &raquo;</title>
	<%@include file="include.jsp" %>
</head>

<body>
	<%@include file="commun.jsp" %>
	
	<div class="contenuAvecMenu contenuAvecOutils">
		<h2>Recherche de &laquo; <%=q%> &raquo;</h2>
		
		<% if (CollectionUtils.isNotEmpty(resultBooks)) { %>
			<h3><%=resultBooks.size()%> livre(s)</h3>
			<div class="boite bleue">
				<ul>
					<% for (Book book : resultBooks) { %>
						<li>
							<a href="category.jsp?id=<%=book.getCategory().getId()%>#BOOK<%=book.getId()%>"><%=book%></a>
							<em>
								(<%=book.getCategory()%>)
							</em>
						</li>
					<% } %>
				</ul>
			</div>
		<% } %>
		
		<% if (CollectionUtils.isNotEmpty(resultCategories)) { %>
			<h3><%=resultCategories.size()%> categorie(s)</h3>
			<div class="boite bleue">
				<ul>
					<% for (Category category : resultCategories) { %>
						<li>
							<a href="category.jsp?id=<%=category.getId()%>"><%=category%></a>
						</li>
					<% } %>
				</ul>
			</div>
		<% } %>
	</div>
</body>
</html>
